package com.erps.CommonUtils;

import com.erps.db.DBManager;

public class DBConfig {
	private DBConfig() {

	}

	public static void setSysKey(String cKeyName, String cKeyValue) {
		JActiveTable aTable = new JActiveTable();
		aTable.addFieldValue("keyvalue", cKeyValue);
		aTable.addFieldValue("keyname", cKeyName);
		aTable.setDbKeyFields("keyname");
		aTable.setDBTableName("client_config");
		aTable.set__rowkeys(cKeyName);
		String cSQL = aTable.getSQL();
		if (DBManager.ExecSQL(cSQL.toString()) == 0) {
			aTable.set__rowkeys("");
			cSQL = aTable.getSQL();
			DBManager.ExecSQL(cSQL);
		}
	}

	public static String getSysKey(String cKeyName, String cKeyValue) {
		StringBuffer sql = new StringBuffer();
		sql.append("select keyvalue ");
		sql.append(" from client_config");
		sql.append(" where keyname='" + cKeyName + "'");
		String cRetValue = DBManager.GetStrValue(sql.toString());
		if (StringEx.isEmpty(cRetValue)) {
			setSysKey(cKeyName, cKeyValue);
		}
		return DBManager.GetStrValue(sql.toString());
	}

	public static String getSysDefVal(String cKeyName) {
		StringBuffer sql = new StringBuffer();
		sql.append("select defvalue ");
		sql.append(" from s_constant");
		sql.append(" where keyname='" + cKeyName + "'");
		return DBManager.GetStrValue(sql.toString());
	}

	public static String getOrgKey(String cOrgID, String cKeyName, String cKeyValue) {
		StringBuffer sql = new StringBuffer();
		sql.append("select keyvalue ");
		sql.append(" from s_orginf_config");
		sql.append(" where orgid='" + cOrgID + "'");
		sql.append(" and keyname='" + cKeyName + "'");
		String cv = DBManager.GetStrValue(sql.toString());
		if ((cv == null) || (cv.length() == 0)) {
			setOrgKey(cOrgID, cKeyName, cKeyValue);
		}
		return DBManager.GetStrValue(sql.toString());
	}

	public static void setOrgKey(String cOrgID, String cKeyName, String cKeyValue) {
		JActiveTable aTable = new JActiveTable();
		aTable.addFieldValue("orgid", cOrgID);
		aTable.addFieldValue("keyname", cKeyName);
		aTable.addFieldValue("keyvalue", cKeyValue);
		aTable.setDbKeyFields("keyname,orgid");
		aTable.setDBTableName("s_orginf_config");
		aTable.set__rowkeys(cKeyName + "," + cOrgID);
		String cSQL = aTable.getSQL();
		if (DBManager.ExecSQL(cSQL.toString()) == 0) {
			aTable.set__rowkeys("");
			cSQL = aTable.getSQL();
			DBManager.ExecSQL(cSQL);
		}
	}

}
